package com.demo.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLIntegrityConstraintViolationException;


@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GolbalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> SQLIntegritExceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.info("捕捉到的异常： {}" , ex.getMessage());
        if(ex.getMessage().contains("Duplicate entry")){
            return R.error("用户名已存在");
        }
        return R.error("未知错误！");
    }

    @ExceptionHandler({CustomException.class})
    public R<String> CustomExceptionHandler(CustomException ex){
        log.info("捕捉到的异常： {}" , ex.getMessage());

        return R.error(ex.getMessage());
    }


}
